package com.sakura.二分答案;

public class Code1283_使结果不超过阈值的最小除数 {

    public int smallestDivisor(int[] nums, int threshold) {
        int left = 0;
        int right = 0;
        for (int num : nums) {
            right = Math.max(right, num);
        }
        while (left + 1 < right) {
            int mid = left + (right - left) / 2;
            if (check(nums, mid, threshold)) {
                right = mid;
            } else {
                left = mid;
            }
        }
        return right;
    }

    private boolean check(int[] nums, int mid, int threshold) {
        long sum = 0;
        for (int num : nums) {
            sum += (num + mid - 1) / mid;
        }

        return sum <= threshold;
    }
}
